//
//  XMPCommondTools.m
//  MonkeyPlayVideo
//
//  Created by jingen zhuang on 2024/6/19.
//

#import "XMPCommondTools.h"

@implementation XMPCommondTools

BOOL MPUIsIPad(void){
    return [[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPad;
}

static CGFloat ned_tatio = 0;
CGFloat MPUAdapted(CGFloat value){
    return MPUAdaptedScaleWidth()*value;
}
CGFloat MPUAdaptedScaleWidth(void){
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    if (ned_tatio==0) {
        if(MPUIsIPad()){
            ned_tatio = (width>height?height:width)/768.0;//9.7
        }else{
            ned_tatio = (width>height?height:width)/375.0;//4.7
        }
    }
    return ned_tatio;
}
static CGFloat ned_tatio_height = 0;
CGFloat MPUAdaptedHeight(CGFloat value){
    return MPUAdaptedScaleHeight()*value;
}
CGFloat MPUAdaptedScaleHeight(void){
    CGFloat width = [UIScreen mainScreen].bounds.size.width;
    CGFloat height = [UIScreen mainScreen].bounds.size.height;
    if (ned_tatio_height==0) {
        if(MPUIsIPad()){
            ned_tatio_height = (width>height?width:height)/1024.0;//9.7
        }else{
            ned_tatio_height = (width>height?width:height)/667.0;//4.7
        }
    }
    return ned_tatio_height;
}


+ (UIColor *) colorFromHexString:(NSString *)hexString {
    NSString *cleanString = [hexString stringByReplacingOccurrencesOfString:@"#" withString:@""];
{
        if([cleanString length] == 3)
        {
            cleanString = [NSString stringWithFormat:@"%@%@%@%@%@%@",
                           [cleanString substringWithRange:NSMakeRange(0, 1)],[cleanString substringWithRange:NSMakeRange(0, 1)],
                           [cleanString substringWithRange:NSMakeRange(1, 1)],[cleanString substringWithRange:NSMakeRange(1, 1)],
                           [cleanString substringWithRange:NSMakeRange(2, 1)],[cleanString substringWithRange:NSMakeRange(2, 1)]];
        }
    }
    if([cleanString length] == 6)
    {
        cleanString = [cleanString stringByAppendingString:@"ff"];
    }
    unsigned int baseValue;
    [[NSScanner scannerWithString:cleanString] scanHexInt:&baseValue];
    float red = ((baseValue >> 24) & 0xFF)/255.0f;
    float green = ((baseValue >> 16) & 0xFF)/255.0f;
    float blue = ((baseValue >> 8) & 0xFF)/255.0f;
    float alpha = ((baseValue >> 0) & 0xFF)/255.0f;
    return [UIColor colorWithRed:red green:green blue:blue alpha:alpha];
}

@end
